package smartcommunityserver.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import smartcommunityserver.bean.*;
import smartcommunityserver.service.NumcountService;
import smartcommunityserver.service.TrafficChannelStatsService;
import smartcommunityserver.service.TrafficChannelStatsService2;

import java.util.List;

@RestController
@RequestMapping("/sm/community")
public class CarCountController {

    // 常量定义：默认日期值
    private static final String DATE_DEFAULT_VALUE = "default";

    @Autowired
    public TrafficChannelStatsService trafficChannelStatsService;

    @RequestMapping("/carCount")
    public JSONObject getCarCount() {


        // 2. 调用Service层获取车辆统计数据
        List<CarCountBean> carCountList = trafficChannelStatsService.getCarCount();

        // 3. 构建JSON结构（三层嵌套：status, msg, data -> series -> data）
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONArray arr1 = new JSONArray();
        resultJson.put("data", arr1);


        // 4. 填充数据

        for (CarCountBean carCount : carCountList) {
            JSONObject dataItem = new JSONObject();
            dataItem.put("name", carCount.getName());
            dataItem.put("value", carCount.getZb());


            arr1.add(dataItem);
        }

        return resultJson;
    }

    @Autowired
    public TrafficChannelStatsService2 trafficChannelStatsService2;
    @RequestMapping("/carCount2")
    public JSONObject getCarCount2() {
        List<CarCountBean2> carCountList = trafficChannelStatsService2.getCarCount2();

        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");


        JSONObject jsonObject = new JSONObject();
        resultJson.put("data", jsonObject);

        JSONArray objects1 = new JSONArray();

        jsonObject.put("categories", objects1);

        for (CarCountBean2 carCountBean2 : carCountList) {
            objects1.add(carCountBean2.getTime());
        }

        JSONArray objects2 = new JSONArray();

        jsonObject.put("series", objects2);

        JSONObject jsonObject1 = new JSONObject();

        objects2.add(jsonObject1);
        jsonObject1.put("name","出行次数");

        JSONArray objects3 = new JSONArray();
        for (CarCountBean2 carCountBean2 : carCountList) {
            objects3.add(carCountBean2.getTotal());
        }
        jsonObject1.put("data", objects3);

        return resultJson;
    }


    @Autowired
    public NumcountService numcountService;

    @RequestMapping("/numcountService")
    public JSONObject getNumcountService() {
        List<NumcountBean> carCount2 = numcountService.getCarCount2();
        JSONObject resultJson = new JSONObject();
        resultJson.put("status", 0);
        resultJson.put("msg", "");

        JSONArray objects = new JSONArray();
        resultJson.put("data",objects);

        for (NumcountBean numcountBean : carCount2) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("name",numcountBean.getName());
            jsonObject.put("value",numcountBean.getNum());
            objects.add(jsonObject);
        }

        return resultJson;
    }
}
